package com.cskaoyan.dbutils;

import com.cskaoyan.connectionpool.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 创建日期: 2022/03/24 11:26
 *
 * @author ciggar
 */
public class QueryRunnerDemo1 {

    static DataSource dataSource;

    static {

        dataSource = DruidUtils.getDataSource();

    }

    public static void main1(String[] args) throws SQLException {

        // 无参构造
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = DruidUtils.getConnection();

        // 有参构造
        QueryRunner queryRunnerWithDatasource = new QueryRunner(dataSource);

        String sql = "insert into account values (?,?,?,?)";
        int affectedRows = queryRunner.update(connection,sql,null,"晁盖",100000,"晁太保");
        System.out.println(affectedRows);

        connection.close();

    }

    public static void main2(String[] args) throws SQLException {

        // 有参构造
        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql = "insert into account values (?,?,?,?)";
        int affectedRows = queryRunner.update(sql,null,"鲁智深",100,"花和尚");
        System.out.println(affectedRows);


    }

    public static void main(String[] args) throws SQLException {

        // 有参构造
        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql = "update account set money = ? where id > ?";
        int affectedRows = queryRunner.update(sql,999,5);
        System.out.println(affectedRows);

//        Connection connection = DruidUtils.getConnection();
//        PreparedStatement preparedStatement = connection.prepareStatement("update account set money = ? where id > ?");
//        preparedStatement.setInt(1,999);
//        preparedStatement.setInt(2,5);
//        int affectedRows = preparedStatement.executeUpdate();
//        System.out.println(affectedRows);




    }
}
